package com.liangzhong.proxy.bag
{
	import com.adobe.serialization.json.JSON;
	import com.greensock.TweenLite;
	import com.liangzhong.commandconst.bag.SMDBag;
	import com.liangzhong.commandconst.logic.CMD_Logic;
	import com.liangzhong.commandconst.pub.CMD_Public;
	import com.liangzhong.constants.HttpRequestConst;
	import com.liangzhong.errortip.ErrorTips;
	import com.liangzhong.global.CMD_Head;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.bag.BagServerDataParse;
	import com.liangzhong.proxy.BaseProxy;
	import com.liangzhong.proxy.ProxyUtils;
	import com.liangzhong.proxyconstant.bag.BagConst;
	import com.socket.clientsocket.IClientSocket;
	
	import flash.net.URLVariables;
	import flash.utils.ByteArray;
	
	import lz.controls.Alert;
	
	/**
	 * 背包proxy 
	 * @author lake
	 * 
	 */	
	public class BagProxy extends BaseProxy
	{
		/**
		 *proxy名字 
		 */
		public static const PROXYNAME:String = "BagProxy";
		
		public function BagProxy(proxyName:String=PROXYNAME, data:Object=null)
		{
			super(proxyName, data);
		}
		/**
		 *接受 
		 * @param mainCmdID
		 * @param subCmdID
		 * @param buffer
		 * @param dataSize
		 * @param clientSocket
		 * @return 
		 * 
		 */		
		override public function onSocketRead(mainCmdID:uint, subCmdID:uint, buffer:ByteArray, dataSize:int, clientSocket:IClientSocket):Boolean
		{
			switch(mainCmdID)
			{
				case CMD_Logic.MAIN://背包协议
				{
					return onDeal(mainCmdID,subCmdID,buffer,dataSize);
				}
				case CMD_Public.MAIN://公用协议
				{
					return onDealPub(mainCmdID,subCmdID,buffer,dataSize);
				}
			}
			return false;
		}
		/**
		 *处理 
		 * @param mainCmdID
		 * @param subCmdID
		 * @param buffer
		 * @param dataSize
		 * @return 
		 */		
		private function onDealPub(mainCmdID:uint,
								subCmdID:uint,
								buffer:ByteArray, 
								dataSize:int):Boolean
		{
			var retcode :int;
			var len		:int;
			var pid		:int;
			var type	:int;
			var oper	:int;
			var locked	:int;
			var id		:int;
			switch(subCmdID)
			{
				case CMD_Public.SUB_USE_ITEM://使用道具
				{
					retcode = buffer.readInt();
					if(retcode == 0)//使用成功
					{
						Alert.showErrorTip("使用成功");
					}
					else
					{
						Alert.showErrorTip(ErrorTips.instance.getCErrorTip(retcode) + "");
					}
					return true;
				}
				case CMD_Public.SUB_CHANGE_ITEM://道具修改
				{
					retcode = buffer.readInt();
					if(retcode == 0)
					{
						len = buffer.readShort();
						for(var i:int = 0;i<len;i++)
						{
							pid 	= buffer.readShort();
							id		= buffer.readUnsignedInt();
							type 	= buffer.readByte();
							oper	= buffer.readByte();
							locked	= buffer.readByte();
							trace(pid,id,type,oper,locked);
							AppData.getInstance().bagDataManager.changeGoods(pid,oper,type,id,locked);
						}
					}
					else
					{
						Alert.showErrorTip(ErrorTips.instance.getCErrorTip(retcode) + "");
					}
					return true;
				}
				case CMD_Public.SUB_ITEM_RESOLVE://物品分解
				{
					if(retcode == 0)//使用成功
					{
						Alert.showErrorTip("分解成功");
					}
					else
					{
						Alert.showErrorTip(ErrorTips.instance.getCErrorTip(retcode) + "");
					}
					return true;
				}
			}
			return false;
		}
		/**
		 *处理 
		 * @param mainCmdID
		 * @param subCmdID
		 * @param buffer
		 * @param dataSize
		 * @return 
		 */		
		private function onDeal(mainCmdID:uint,
								subCmdID:uint,
								buffer:ByteArray, 
								dataSize:int):Boolean
		{
			var resObj :Object;
			var retcode :int;
			switch(subCmdID)
			{
				case SMDBag.SUB_GRADE_UP://升级
				{
					resObj = BagServerDataParse.readGradeUp(buffer);
					this.sendNotification(BagConst.GRADE_UP_RESULT,resObj);
					return true;
				}
				case SMDBag.SUB_OPEN_CELLS://开格子
				{
					resObj = BagServerDataParse.readOpenCells(buffer);
					this.sendNotification(BagConst.OPEN_CELLS_RESULT,resObj);
					return true;
				}
			}
			return false;
		}
		/**
		 * 升级
		 */		
		public function gradeUp():void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size,CMD_Logic.MAIN,SMDBag.SUB_GRADE_UP,0);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *使用物品 
		 * @param pid
		 * 
		 */		
		public function useItem(pid :int):void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 2,CMD_Public.MAIN,CMD_Public.SUB_USE_ITEM,0);
			sendBuf.writeShort(pid);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *开格子 
		 * 
		 */		
		public function openCell():void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size,CMD_Logic.MAIN,SMDBag.SUB_OPEN_CELLS,0);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *分解物品 
		 * @param pid
		 * 
		 */		
		public function resolveItem(pid :int):void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 2,CMD_Public.MAIN,CMD_Public.SUB_ITEM_RESOLVE,0);
			sendBuf.writeShort(pid);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *获取仓库基本信息 
		 */		
		public function getBagBaseInfo():void
		{
			var variables:URLVariables = ProxyUtils.instance.getUrlVariables();
			variables.method = HttpRequestConst.GET_BAG_INFO;
			ProxyUtils.instance.webClient(getBagBaseInfoRes,variables,false);
		}
		/**
		 * 获取仓库基本信息  res
		 * @param data
		 * 
		 */		
		private function getBagBaseInfoRes(data :Object):void
		{
			var str:String = String(data);
			var results:Array = JSON.decode(str);
			var obj:Object = results[0];
			if(obj.retcode == 0)
			{
				AppData.getInstance().bagDataManager.bagInfo.parseBaseInfo(obj.knapsack);
				AppData.getInstance().bagDataManager.parsePropInfo(obj.propinfo);
			}
			else
			{
				Alert.showErrorTip("仓库信息 code = " + obj.retcode);
			}
		}
	}
}